<template>
  <div class="footer">
    <div class="footer__amount">
      <span class="footer__amount__info">实付金额</span>
      <span class="footer__amount__price"
        >¥<i>{{ calculations.price }}</i></span
      >
    </div>
    <div class="footer__btn">
      <button @click="() => handleShowConfirmChange(true)">提交订单</button>
    </div>
  </div>
  <div
    class="mask"
    v-show="showPopout"
    @click="() => handleShowConfirmChange(false)"
  >
    <div class="mask__popout" @click.stop>
      <h3 class="mask__popout__ask">确认要离开收银台？</h3>
      <p class="mask__popout__desc">请尽快完成支付，否则将被取消</p>
      <div class="mask__popout__btn">
        <button
          class="mask__popout__btn__cancel"
          @click="() => handlePopoutBtn(true)"
        >
          取消订单
        </button>
        <div class="mask__gap"></div>
        <button
          class="mask__popout__btn__ack"
          @click="() => handlePopoutBtn(false)"
        >
          确认支付
        </button>
      </div>
    </div>
  </div>
</template>

<script>
import { ref } from "vue";
import { useRouter, useRoute } from "vue-router";
import { useStore } from "vuex";
import { post } from "../../utils/request";
import { usecommonCartEffect } from "../../effects/cartEffects";

//下单相关逻辑
const userMakeOrderEffect = (shopId, shopName, productsList) => {
  const router = useRouter();
  const store = useStore();
  const handlePopoutBtn = async (isCanceled) => {
    const products = [];
    for (let i in productsList.value) {
      const product = productsList.value[i];
      products.push({
        id: parseInt(product._id, 10),
        num: product.count,
      });
    }
    try {
      const result = await post("/api/order", {
        addressId: 1,
        shopId,
        shopName: shopName.value,
        isCanceled,
        products,
      });
      if (result?.errno === 0) {
        store.commit("clearCartData", shopId);
        //利用路由的push方法跳转到指定的页面
        router.push({ name: "OrderList" });
      }
    } catch (e) {
      //调用已经封装好的弹窗函数方法.
      showToast("下单失败");
    }
  };
  return { handlePopoutBtn };
};

//蒙层展示相关的逻辑
const useShowMaskEffect = () => {
  const showPopout = ref(false);
  const handleShowConfirmChange = (status) => {
    showPopout.value = status;
  };
  return { showPopout, handleShowConfirmChange };
};
export default {
  name: "Amount",
  setup() {
    const route = useRoute();
    const shopId = parseInt(route.params.id, 10);
    const { calculations, shopName, productsList } =
      usecommonCartEffect(shopId);
    const { handlePopoutBtn } = userMakeOrderEffect(
      shopId,
      shopName,
      productsList
    );
    const { showPopout, handleShowConfirmChange } = useShowMaskEffect();
    return {
      showPopout,
      handleShowConfirmChange,
      calculations,
      handlePopoutBtn,
    };
  },
};
</script>

<style lang="scss" scoped>
@import "../../style/viriables.scss";
.footer {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 0.49rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: $content-fzcolor;
  &__amount {
    flex: 1;
    padding-left: 0.24rem;
    span {
      font-size: 0.14rem;
      color: $content-fontcolor;
      line-height: 0.2rem;
    }
    &__info {
      margin-right: 0.053rem;
    }
    &__price {
      font-weight: bold;
    }
  }
  &__btn {
    width: 0.98rem;
    height: 100%;
    button {
      width: 100%;
      height: 100%;
      font-size: 0.14rem;
      color: $content-fzcolor;
      background-color: $cart-bgcolor;
    }
  }
}
.mask {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  z-index: 1;
  &__gap {
    width: 0.24rem;
  }
  &__popout {
    width: 3.01rem;
    height: 1.57rem;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: $content-fzcolor;
    border-radius: 0.04rem;
    &__ask {
      font-size: 0.18rem;
      color: $content-fontcolor;
      line-height: 0.25rem;
      font-weight: bold;
      text-align: center;
      margin: 0.24rem 0 0.08rem;
    }
    &__desc {
      font-size: 0.14rem;
      color: $content-numcolor;
      line-height: 0.2rem;
      text-align: center;
      margin-bottom: 0.24rem;
    }
    &__btn {
      display: flex;
      justify-content: center;
      &__cancel {
        font-size: 0.14rem;
        color: $ibcrease-bgcolor;
        padding: 0.06rem 0.12rem;
        border: 0.01rem solid $cart-bgcolor;
        background-color: $content-fzcolor;
        border-radius: 0.16rem;
      }
      &__ack {
        font-size: 0.14rem;
        color: $content-fzcolor;
        padding: 0.06rem 0.12rem;
        border: 0.01rem solid $cart-bgcolor;
        background-color: $cart-bgcolor;
        border-radius: 0.16rem;
      }
    }
  }
}
</style>